/*
  desc: 增减数量
*/
<template>
  <div class="cartcontrol">
    <transition name="decrease">
      <div
        v-show="food.count>0"
        class="cart-decrease icon-remove_circle_outline"
        @click.stop="decreaseCart"
      ></div>
    </transition>

    <div class="cart-count" v-show="food.count>0">{{food.count}}</div>
    <div class="cart-add icon-add_circle" @click.stop="addCart"></div>
  </div>
</template>
<script>
import Vue from "vue";
export default {
  name: "cartcontrol",
  props: {
    food: {
      type: Object
    }
  },
  components: {},
  data() {
    return {};
  },
  methods: {
    addCart() {
      if (!this.food.count) {
        // 因为此时传进来的food没有count属性，所以要用Vue.set添加该属性
        Vue.set(this.food, "count", 1);
        this.food.count = 1;
      } else {
        this.food.count++;
      }
      // console.log(1);
    },
    decreaseCart() {
      this.food.count--;
      console.log(this.food.count)
    }
  },
  created() {
    // console.log(this.food);
  }
};
</script>
<style lang='stylus' scoped>
.cartcontrol {
  font-size: 0;

  .cart-decrease, .cart-add {
    display: inline-block;
    font-size: 20px;
    padding: 6px;
    box-sizing: border-box;
    color: rgb(0, 160, 220);
  }

  .cart-count {
    display: inline-block;
    width: 12px;
    height: 24px;
    line-height: 24px;
    text-align: center;
    padding-top: 6px;
    font-size: 10px;
    vertical-align: top;
    color: rgb(147, 153, 159);
  }

  .cart-add {
    display: inline-block;
  }

  // 减少按钮的动画效果
  .decrease-enter-active, .decrease-leave-active {
    opacity: 1;
    transition: all 1s ease;
    transform: translate3d(0, 0, 0) rotate(0);
  }

  .decrease-enter, .decrease-leave-to {
    opacity: 0;
    transform: translate3d(24px, 0, 0) rotate(880deg);
  }
}
</style>
